The PrintDialog control is used to open the Windows Print Dialog. A PrintDocument component allows users to send an output to a printer. By the help of PrintPreviewDialog, You can preview a document.
How to PrintDocument and check PrintPreview in VB.Net.
Drag and drop PrintDocument control, PrintDialog control and PrintPreviewDialog control from the toolbox on the WindowForm.
PrintDocument: The PrintDocument object encapsulates all the information needed to print a page. They associate with the control which content can be print. They handle the events and operations of printing.
Step1: Associate control to print the document
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
e.Graphics.DrawString(Label1.Text, Label1.Font, Brushes.Black, 100, 100)
e.Graphics.PageUnit = GraphicsUnit.Inch
End Sub
Step2: Write code for printing and print preview.
'open the print dialog on Print Button click
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
PrintDialog1.Document = PrintDocument1 'PrintDialog associate with PrintDocument.
If PrintDialog1.ShowDialog() = DialogResult.OK Then
PrintDocument1.Print()
End If
'open the print preview on PrintPreview Button click
Private Sub btnPrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintPreview.Click
PrintPreviewDialog1.Document = PrintDocument1 'PrintPreviewDialog associate with PrintDocument.
PrintPreviewDialog1.ShowDialog() 'open the print preview
End Sub
Run the project
When you click the Print Preview button then the preview will show.
When clicking the print button then the print dialog will open.
When you click print then the output will go to the printer.
[You should Read also this Article - ToolBarTray Control in WPF ]
Anonymous User
11-Feb-2019The article was helpful.
Dim Christop
13-Sep-2012Anonymous User
13-Oct-2011It's really helped me to complete my project.
Thanks